/*
 *所有关于Fk_yusuan类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*Fk_yusuanService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.MyTable.IRepositories;
using VOL.Core.ManageUser;
using VOL.Core.DBManager;
using System.Collections.Generic;
using VOL.MyTable.IServices;
using System.Collections;
using VOL.MyTable.Repositories;

namespace VOL.MyTable.Services
{
    public partial class Fk_yusuanService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IFk_yusuanRepository _repository;//访问数据库
        private readonly Fk_reimService _Fk_reimService;
        [ActivatorUtilitiesConstructor]
        public Fk_yusuanService(
            IFk_yusuanRepository dbRepository,
            IHttpContextAccessor httpContextAccessor,
            Fk_reimService reimService
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            _Fk_reimService = reimService;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        public override PageGridData<Fk_yusuan> GetPageData(PageDataOptions options)
        {
            SummaryExpress = (IQueryable<Fk_yusuan> queryable) =>
            {
                return queryable.GroupBy(x => 1).Select(x => new
                {
                    //AvgPrice注意大小写和数据库字段大小写一样
                    tsl01 = x.Sum(o => o.tsl01).ToString("f2"),
                    tsl02 = x.Sum(o => o.tsl02).ToString("f2"),
                    tsl03 = x.Sum(o => o.tsl03).ToString("f2"),
                    tsl04 = x.Sum(o => o.tsl04).ToString("f2"),
                    tsl05 = x.Sum(o => o.tsl05).ToString("f2"),
                    tsl06 = x.Sum(o => o.tsl06).ToString("f2"),
                    tsl07 = x.Sum(o => o.tsl07).ToString("f2"),
                    tsl08 = x.Sum(o => o.tsl08).ToString("f2"),
                    tsl09 = x.Sum(o => o.tsl09).ToString("f2"),
                    tsl10 = x.Sum(o => o.tsl10).ToString("f2"),
                    tsl11 = x.Sum(o => o.tsl11).ToString("f2"),
                    tsl12 = x.Sum(o => o.tsl12).ToString("f2"),

                })
                .FirstOrDefault();
            };

            QueryRelativeExpression = (IQueryable<Fk_yusuan> queryable) =>
            {
                //只有当前用户所属的部门被成本中心授权后才能看到对应的预算
                //IQueryable<int> userQuery = RoleContext.GetCurrentAllChildUser();
               Sys_User user= DBServerProvider.DbContext.Set<Sys_User>().Where(w=>w.UserName== UserContext.Current.UserName).FirstOrDefault();
               if (user.UserName == "admin") return queryable;//如果是管理员全部都能看到
               int? depid_oa = _Fk_reimService.get_oayidepidfrom_userid(user.UserName);//获取用户的一级部门
               int?[] rda_array = DBServerProvider.DbContext.Set<Fk_rcntr_dep_auth>().Where(w => w.depid_oa == depid_oa).Select(w => w.rcntr_id).ToArray();
               
                string[] cntr_array = DBServerProvider.DbContext.Set<Fk_cntr>().Where(w => rda_array.Contains(w.rcntr_id)).Select(w => w.rcntr_no).ToArray();
                queryable = queryable.Where(x => cntr_array.Contains(x.rcntr));
                return queryable;

            };
            var extra = base.GetPageData(options);
            Hashtable rtn_hst = new Hashtable();
            // IQueryable<Fl_template_list> queryeable = repository.DbContext.Set<Fl_template_list>();
            dynamic Fk_flow_list = repository.DbContext.Set<Fk_flow>().Select(s => new { s.rcntr, s.racct }).Distinct().ToList();
            rtn_hst.Add("flows", Fk_flow_list);
            extra.extra = rtn_hst;
            return extra;
        }
    }
}
